package com.yuantu.judiciary.utils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import javax.crypto.Cipher;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import javax.crypto.spec.IvParameterSpec;
import java.security.Key;
import java.util.Base64;

/**
 * @Description 说明类的用途
 * @ClassName DESUtil
 * @Author zzd
 * @Date 2019/10/22 16:09
 * @Version 1.0
 **/
public class DESUtil {

    /**
     * 偏移变量，固定占8位字节
     */
    private final static String IV_PARAMETER = "12345678";
    /**
     * 密钥算法
     */
    private static final String ALGORITHM = "DES";
    /**
     * 加密/解密算法-工作模式-填充模式
     */
//    private static final String CIPHER_ALGORITHM = "DES/CBC/PKCS5Padding";
    private static final String CIPHER_ALGORITHM = "DES/ECB/PKCS5Padding";
    /**
     * 默认编码
     */
    private static final String CHARSET = "utf-8";

    /**
     * 生成key     *
     * * @param password
     * * @return
     * * @throws Exception
     */
    private static Key generateKey(String secret) throws Exception {
        DESKeySpec dks = new DESKeySpec(secret.getBytes(CHARSET));
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM);
        return keyFactory.generateSecret(dks);
    }

    /**
     * DES加密字符串     *
     * * @param password 加密密码，长度不能够小于8位
     * * @param data 待加密字符串
     * * @return 加密后内容
     */
    public static String encrypt(String secret, String data) {
        if (secret == null || secret.length() < 8) {
            throw new RuntimeException("加密失败，key不能小于8位");
        }
        if (data == null) {
            return null;
        }
        try {
            Key secretKey = generateKey(secret);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
            IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET));
            cipher.init(Cipher.ENCRYPT_MODE, secretKey);
            byte[] bytes = cipher.doFinal(data.getBytes(CHARSET));
            //JDK1.8及以上可直接使用Base64，JDK1.7及以下可以使用BASE64Encoder
            // Android平台可以使用android.util.Base64
            return new String(Base64.getEncoder().encode(bytes));
        } catch (Exception e) {
            e.printStackTrace();
            return data;
        }
    }

    /**
     * DES解密字符串     *
     *
     * @param secret 解密密码，长度不能够小于8位
     * @param data   待解密字符串
     * @return 解密后内容
     */
    public static String decrypt(String secret, String data) {
        if (secret == null || secret.length() < 8) {
            throw new RuntimeException("加密失败，key不能小于8位");
        }
        if (data == null) {
            return null;
        }
        try {
            Key secretKey = generateKey(secret);
            Cipher cipher = Cipher.getInstance(CIPHER_ALGORITHM);
//            IvParameterSpec iv = new IvParameterSpec(IV_PARAMETER.getBytes(CHARSET));
            cipher.init(Cipher.DECRYPT_MODE, secretKey);
            return new String(cipher.doFinal(Base64.getDecoder().decode(data.getBytes(CHARSET))), CHARSET);
        } catch (Exception e) {
            e.printStackTrace();
            return data;
        }
    }
//    public static void main(String[] args) {
//        String password="1234567890";
//        String data="hello word";
//        String result=encrypt(password,data);
//        System.out.println(result);
//        String result="VqIDLCYrxSYrvuyfg81UH2VmQBy5q8lthiRqc5D/3ACe8IWUhmDHekPa18DGleR9qbqBpzvuM9DkZLH0TdeJ+1mhEL6u0s9JIgr3S5CHRQi/fcEhv0/+Mg9yyM0eHjnCboMXz9xTmlGsUOX9wDHDHhkEaQ9O5cl1tOkboKR7/KwdIa6m9+FqvOEBYnapleUXF1vZvjTKY5c3dHyZMPvk+4qwjZ2mB9aE6wBclIWi9VjOxo9tVOdLPU4BpzSDXf4g6HKigXqsb/sjxuWmgOFVyGXvdJRd6uS7cqu3xy7vmqX5dYG9tGbK+bSqjIti5jw5l0g6INU1unLaJy1r5h5W+o0n9K3HjLmfTkZ0H05QV7AC2QiFVXomB3N7vHVhMu5iZsxRRpIj4AyKsI2dpgfWhCRqTBuMNVEbuMHkRE2+FplYHdXW8G+WBWBgSlp5HlvWTmaWdos99qYuB6gqBMLYgrz7ihXTWUH9A7FEh/YmlmBRXzjaiY4ewyftmM/WUICUHPChbO0QN3NfCzv1xM55LTQW5RZ043TqCUGyN4JpCvyoMcWBG7bpYVPdEn+MMwLdYJhjk+Hh8zc=";
//        String decResult=decrypt("43EAA3AE9A95467C92EA4543AADF9570",result);
//        System.out.println(decResult);
//    }



    public static void main(String[] args) {
        String json = "{\"username\":\"thirdV1\",\"password\":\"k7e^b9m@z4!d\",\"timestamp\":1750146372}";
        System.out.println(DESUtil.encrypt("a4052b5cf",json));
    }

    public static String getLshFromJson(JSONObject responseJO, String ah) {
        //得到流水号数组
        JSONArray dataArray = responseJO.getJSONArray("data");
//        System.err.println(dataArray);
        String lsh = "";
        //这边的流水号结果返回的是列表，由于不是根据完整的案号查询，所有可能出现，多个结果或是结果的案号与查询的不匹配的情况
        //进行循环判断
        for (int i = 0; i < dataArray.size(); i++) {
            JSONObject jsonObject = dataArray.getJSONObject(i);
            String jsonAh = jsonObject.getString("ah");
            //如果案号相等，说明返回正确
            if (ah.equals(jsonAh)) {
                //设置流水号
                lsh = jsonObject.getString("lsh");
                //停止循环
                break;
            }
        }
        return lsh;

    }








}
